/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.Account;
import hibernate.entity.Notification;
import hibernate.entity.TypeNotification;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Query;

/**
 *
 * @author Tùng
 */
public class NotificationDAO {

    private Session session;

    public NotificationDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public boolean insertNotification(int accountID, int typeID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            TypeNotification tn = (TypeNotification) session.get(TypeNotification.class, typeID);
            boolean accepted = true;
            Notification no = new Notification(acc, tn, accepted);
            session.save(no);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteNotificationbyAccountID(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete Notification where AccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Notification checkNotification(int accountID, int typeID) {
        Notification notification = new Notification();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Notification Where AccountID = ? and TypeID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.setInteger(1, typeID);
            //Object acc = query.uniqueResult();
            notification = (Notification) query.uniqueResult();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return notification;
    }

    public boolean updateNotification(boolean accepted, int noID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Notification no = (Notification) session.get(Notification.class, noID);
            no.setAccepted(accepted);
            session.update(no);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }


}
